package com.example.administrator.nl.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.nl.R;
import com.example.administrator.nl.activity.order.AfterSaleActivity;
import com.example.administrator.nl.activity.order.MyCollectionActivity;
import com.example.administrator.nl.activity.order.MyMakeActivity;
import com.example.administrator.nl.activity.order.MyOrderActivity;
import com.example.administrator.nl.activity.setting.MyAccountActivity;
import com.example.administrator.nl.adapter.MyServerAdapter;
import com.example.administrator.nl.bean.MyServerBean;
import com.example.administrator.nl.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment {
    private String TAG = "MeFragment";
    private Unbinder unbinder;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;

    @BindView(R.id.rl_my_order)
    RelativeLayout rl_my_order;

    // 售后退货
    @BindView(R.id.ll_after_sale)
    LinearLayout ll_after_sale;

    // 积分
    @BindView(R.id.tv_integral_me)
    TextView tv_integral_me;


    private List<MyServerBean> list;
    private MyServerAdapter myServerAdapter;


    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        unbinder = ButterKnife.bind(this, view);
        LogUtil.Log_D(TAG, "onCreateView");
        initView();
        listener();
        return view;
    }

    private void listener() {
        myServerAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ll_label:
                        Intent intent = null;
                        switch (position) {
                            case 0:
                                intent = new Intent(getActivity(), MyAccountActivity.class);
                                startActivity(intent);
                                break;
                            case 4:
                                // 我的预约
                                intent = new Intent(getActivity(), MyMakeActivity.class);
                                startActivity(intent);
                                break;
                            case 5:
                                // 我的收藏
                                intent = new Intent(getActivity(), MyCollectionActivity.class);
                                startActivity(intent);
                                break;
                        }
                        break;
                    default:
                        break;
                }
            }
        });

        rl_my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent);
            }
        });
        ll_after_sale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AfterSaleActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {

        getMyServer();
        recycler_view.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        myServerAdapter = new MyServerAdapter(R.layout.item_my_server_layout, list);
        recycler_view.setAdapter(myServerAdapter);

    }

    private List<MyServerBean> getMyServer() {
        list = new ArrayList<>();
        list.add(new MyServerBean("我的账户"));
        list.add(new MyServerBean("我的团队"));
        list.add(new MyServerBean("推荐有礼"));
        list.add(new MyServerBean("积分商城"));
        list.add(new MyServerBean("我的预约"));
        list.add(new MyServerBean("我的收藏"));
        list.add(new MyServerBean("我的地址"));
        list.add(new MyServerBean("客服中心"));
        list.add(new MyServerBean("设备订单"));
        list.add(new MyServerBean("教育培训"));
        return list;
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.Log_D(TAG, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.Log_D(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.Log_D(TAG, "onPause");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.Log_D(TAG, "onDestroy");

        if (null != unbinder) {
            unbinder.unbind();
            ;
        }
    }
}
